from . import db
class User(db.Model):
    '''user'''
    id        = db.Column(db.Integer, primary_key=True)
    username  = db.Column(db.String(80))
    password  = db.Column(db.String(120))
    telephone = db.Column(db.String(30), unique=True)
    email     = db.Column(db.String(30))
    qq        = db.Column(db.String(30))
    def __init__(self, username='',password='',telephone='',email='',qq=''):
        self.username  = username
        self.password  = password
        self.email     = email
        self.telephone = telephone
        self.qq        = qq
    def __repr__(self):
        return self.username

class Site(db.Model):
    '''site for mqtt client'''
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))
    cid = db.Column(db.String(30))
    lora_addr = db.Column(db.String(30))
    number = db.Column(db.String(30))
    version = db.Column(db.String(10))
    position = db.Column(db.String(100))
    status = db.Column(db.String(30))
    alive = db.Column(db.Boolean)
    add_time = db.Column(db.DateTime)

    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    user = db.relationship('User', backref='site')

class Node(db.Model):
    '''lora of nodes '''
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80))
    cid = db.Column(db.String(30))
    lora_addr = db.Column(db.String(30))
    number = db.Column(db.String(30))
    version = db.Column(db.String(10))
    position = db.Column(db.String(100))
    status = db.Column(db.String(30))
    # 阀门状态
    tap_status = db.Column(db.Boolean)
    alive = db.Column(db.Boolean)
    alive_time = db.Column(db.DateTime)
    add_time = db.Column(db.DateTime)

    site_id = db.Column(db.Integer, db.ForeignKey('site.id'))
    site = db.relationship('Site', backref='node')

    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    user = db.relationship('User', backref='node')

class NodeRecord(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    operator = db.Column(db.String(100))
    operation = db.Column(db.String(100))
    time = db.Column(db.DateTime)
    node_id = db.Column(db.Integer, db.ForeignKey('node.id'))
    node = db.relationship('Node', backref='noderecord')

